package com.example.socialapp.domain.usecase.auth

import com.example.socialapp.domain.model.User
import com.example.socialapp.domain.repository.AuthRepository
import javax.inject.Inject

/**
 * Use case for user login
 */
class LoginUseCase @Inject constructor(
    private val authRepository: AuthRepository
) {
    /**
     * Login with email and password
     *
     * @param email User email
     * @param password User password
     * @param rememberMe Whether to remember the user
     * @return Result containing User on success or Exception on failure
     */
    suspend operator fun invoke(
        email: String,
        password: String,
        rememberMe: Boolean
    ): Result<User> {
        if (email.isBlank()) {
            return Result.failure(IllegalArgumentException("Email cannot be empty"))
        }
        
        if (!isValidEmail(email)) {
            return Result.failure(IllegalArgumentException("Invalid email format"))
        }
        
        if (password.isBlank()) {
            return Result.failure(IllegalArgumentException("Password cannot be empty"))
        }
        
        if (password.length < 6) {
            return Result.failure(IllegalArgumentException("Password must be at least 6 characters"))
        }
        
        return authRepository.login(email, password, rememberMe)
    }
    
    /**
     * Validate email format
     *
     * @param email Email to validate
     * @return Boolean indicating if email is valid
     */
    private fun isValidEmail(email: String): Boolean {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
    }
} 